package com.imicode.hotel.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.imicode.hotel.service.FoodTypeService;
import com.imicode.hotel.vo.FoodType;

@WebServlet("/foodtype.view")
public class FoodTypeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	private FoodTypeService service = new FoodTypeService();
       

    public FoodTypeServlet() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		
		//1. 获取参数
		String method = request.getParameter("method");
		
		System.out.println("method: " + method);
		//2. 根据参数执行对应的逻辑
		if("list".equals(method)) {
			list(request,response);
		} else if ("addpage".equals(method)) {
			addpage(request, response);
		} else if("add".equals(method)) {
			//1. 获取菜系名称
			String name = request.getParameter("name");
			System.out.println("-----------------菜系名称：" + name);
			//2. 保存菜系
			service.save(name);
			//3. 跳转到列表
			response.sendRedirect("foodtype.view?method=list");
		} else if("editpage".equals(method)) {
			//1. 获取id
			int id = Integer.valueOf(request.getParameter("id"));
			//2. 根据ID查询到对应的菜系
			FoodType foodType = service.get(id);
			//3. 把菜系传递给页面
			request.setAttribute("foodType", foodType);
			//4. 跳转到更新页面
			RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/jsp/foodtype/edit.jsp");
			dispatcher .forward(request, response);
		} else if("edit".equals(method)) {
			//1. 获取菜系名称
			int id = Integer.valueOf(request.getParameter("id"));
			String name = request.getParameter("name");
			//2. 保存菜系
			service.edit(id,name);
			//3. 跳转到列表
			response.sendRedirect("foodtype.view?method=list");	
		} else if("delete".equals(method)) {
			//1. 获取菜系ID
			int id = Integer.valueOf(request.getParameter("id"));
			//2. 删除对应的菜系
			service.delete(id);
			//3. 跳转到列表
			response.sendRedirect("foodtype.view?method=list");
		}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
	
	private void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException  {
		//1. 获取菜系名称
		String name = request.getParameter("name");
		//2. 数据库获取数据
		List<FoodType> foodTypeList = service.list(name);
		//3. 把数据传给页面
		request.setAttribute("list", foodTypeList);
		//4. 跳转到列表页面
		RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/jsp/foodtype/list.jsp");
		dispatcher .forward(request, response);
	}
	
	private void addpage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException  {
		//跳转到添加页面
		RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/jsp/foodtype/add.jsp");
		dispatcher .forward(request, response);
	}

}
